﻿#region

using System;
using System.Collections.Generic;
using System.Web.UI;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class StartBooking : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int bookingId;
            if (int.TryParse(Request.QueryString["bookingId"], out bookingId))
            {
                if (IsPostBack) return;
                string bookingType = BookingBLL.GetBookingType(bookingId);
                switch (bookingType)
                {
                    case null:
                        Response.Redirect("Default.aspx");
                        break;
                    case "PICK & DROP":
                    {
                        PickAndDropBooking booking = PickAndDropBookingBLL.GetBooking(bookingId);
                        if (booking.SupervisorId == null) Response.Redirect("Default.aspx");
                        dvPickAndDrop.Visible = true;
                        dvPickAndDrop.DataSource = new List<PickAndDropBooking> {booking};
                        dvPickAndDrop.DataBind();
                    }
                        break;
                    default:
                    {
                        PickBooking booking = PickBookingBLL.GetBooking(bookingId);
                        if (booking.SupervisorId == null) Response.Redirect("Default.aspx");
                        dvPick.Visible = true;
                        dvPick.DataSource = new List<PickBooking> {booking};
                        dvPick.DataBind();
                    }
                        break;
                }
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void ibReject_OnClick(object sender, ImageClickEventArgs e)
        {
            BookingBLL.ChangeStatus(Convert.ToInt32(Request.QueryString["bookingId"]), "Rejected");
            Response.Redirect("Default.aspx");
        }

        protected void ibStart_OnClick(object sender, ImageClickEventArgs e)
        {
            int bookingId = Convert.ToInt32(Request.QueryString["bookingId"]);

            BookingBLL.ChangeStatus(bookingId, "OnProgress");

            foreach (
                Booking_Vehicle_Driver bvd in Booking_Vehicle_DriverBLL.GetVehicleAndDriverDetailsByBookingId(bookingId)
                )
            {
                DriverBLL.SetAvailability(bvd.DriverId, false);
                VehicleBLL.SetAvailability(bvd.VehicleRegistrationNo, false);
            }

            Response.Redirect("Default.aspx");
        }

        protected void ibReConfirm_OnClick(object sender, ImageClickEventArgs e)
        {
            int bookingId = Convert.ToInt32(Request.QueryString["bookingId"]);
            BookingBLL.ResetConfirmation(bookingId);
            Response.Redirect("ConfirmBooking.aspx?bookingId=" + bookingId);
        }
    }
}